package com.china08.yxyapi.api.home.kecheng.xiaoben;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonAuth;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonAuthRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;

/**
 * 校本课程--详情页--推荐课程
 * 
 * 推荐同贡献者的2个课程，如没有同贡献者的就推荐同分类下的2个课程
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengXiaobenParticularRecommendSrhController
        extends YxyApiBaseController {

    @Autowired
    private YxyLessonsSchoolLessonRepository yxyLessonsSchoolLessonRepository;

    @Autowired
    private YxyLessonsSchoolCatalogRepository yxyLessonsSchoolCatalogRepository;

    @Autowired
    private YxyLessonsSchoolLessonAuthRepository yxyLessonsSchoolLessonAuthRepository;

    @Autowired
    JdbcTemplate JdbcTemplate;

    @RequestMapping(value = "/api_yw/home/kecheng/xiaoben/particular/recommend/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> find(HttpServletRequest request,
            HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String lessonId = reqModel.lessonId;
        // String userId = getProfile(request, response).getId();
        // String roleId = getProfile(request, response).getRoleId();
        String schoolId = getProfile(request, response).getSchoolId();
        List<String> studentList = reqModel.studentList;

        List<RespModel> retuLst = new ArrayList<>();

        YxyLessonsSchoolLesson yxyLessonsSchoolLessons = yxyLessonsSchoolLessonRepository
                .findById(lessonId);
        // if (null != yxyLessonsSchoolLessons) {
        // String aaName = yxyLessonsSchoolLessons.getAuthor();
        // List<YxyLessonsSchoolLesson> yxyLessonsSchoolLessonLst =
        // yxyLessonsSchoolLessonRepository
        // .findTop2ByAuthorAndStatusAndIsVisibleOrderByVvDesc(aaName,
        // 2, 1);
        //
        // // 同贡献者的2个课程
        // if (yxyLessonsSchoolLessonLst.size() >= 2) {
        // for (YxyLessonsSchoolLesson yxyLessonsSchoolLesson :
        // yxyLessonsSchoolLessonLst) {
        // RespModel model = new RespModel();
        //
        // List<YxyLessonsSchoolLessonAuth> userLst =
        // yxyLessonsSchoolLessonAuthRepository
        // .findBySchoolIdAndLessonIdAndUserIdIn(schoolId,
        // yxyLessonsSchoolLesson.getId(),
        // studentList);
        // if (userLst.size() > 0) {
        // model.userVip = true;
        // } else {
        // model.userVip = false;
        // }
        // model.vip = yxyLessonsSchoolLesson.getVip();
        // model.cover = yxyLessonsSchoolLesson.getCover();
        // model.count = yxyLessonsSchoolLesson.getCount();
        // model.duration = yxyLessonsSchoolLesson.getDuration();
        // model.name = yxyLessonsSchoolLesson.getName();
        // model.author = yxyLessonsSchoolLesson.getAuthor();
        // model.vv = yxyLessonsSchoolLesson.getVv();
        // model.lessonId = yxyLessonsSchoolLesson.getId();
        //
        // YxyLessonsSchoolCatalog yxyLessonsSchoolCatalogs =
        // yxyLessonsSchoolCatalogRepository
        // .findById(yxyLessonsSchoolLesson.getCatalogId2());
        // model.twoName = yxyLessonsSchoolCatalogs.getName();
        // retuLst.add(model);
        // }
        // }
        // }
        // 同分类下的2个课程
        // else {

        List<YxyLessonsSchoolLesson> yxyLessonsSchoolLessonLst1 = yxyLessonsSchoolLessonRepository
                .findTop2ByCatalogId2AndStatusAndIsVisibleOrderByVvDesc(
                        yxyLessonsSchoolLessons.getCatalogId2(), 2, 1);
        if (yxyLessonsSchoolLessonLst1.size() >= 2) {
            for (YxyLessonsSchoolLesson yxyLessonsSchoolLesson2 : yxyLessonsSchoolLessonLst1) {
                RespModel model = new RespModel();

                List<YxyLessonsSchoolLessonAuth> userLst = yxyLessonsSchoolLessonAuthRepository
                        .findBySchoolIdAndLessonIdAndUserIdIn(schoolId,
                                yxyLessonsSchoolLesson2.getId(), studentList);
                if (userLst.size() > 0) {
                    model.userVip = true;
                } else {
                    model.userVip = false;
                }

                model.vip = yxyLessonsSchoolLesson2.getVip();
                model.cover = yxyLessonsSchoolLesson2.getCover();
                model.count = yxyLessonsSchoolLesson2.getCount();
                model.duration = yxyLessonsSchoolLesson2.getDuration();
                model.name = yxyLessonsSchoolLesson2.getName();
                model.author = yxyLessonsSchoolLesson2.getAuthor();
                model.vv = yxyLessonsSchoolLesson2.getVv();

                YxyLessonsSchoolCatalog yxyLessonsSchoolCatalogs = yxyLessonsSchoolCatalogRepository
                        .findById(yxyLessonsSchoolLesson2.getCatalogId2());
                model.twoName = yxyLessonsSchoolCatalogs.getName();
                retuLst.add(model);
            }
        }

        // }

        return retuLst;
    }

    // public List<String> studentIdList(String userId) {
    // List<String> list = new ArrayList<>();
    // String sql = " select student_id from
    // china08_yunxiao_pro.rel_student_guardian where guardian_id = ? ";
    // List<Map<String, Object>> listMaps = JdbcTemplate.queryForList(sql, new
    // Object[] { userId });
    // for (Map<String, Object> map : listMaps) {
    // list.add(map.get("student_id") + "");
    // }
    // return list;
    // }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 课程ID
        public String lessonId;
        // 用户Id
        public List<String> studentList;

    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 二级分类名称
        public String twoName;
        // vip
        public Integer vip;
        // userVip
        public Boolean userVip;
        // 封面
        public String cover;
        // 总课时
        public Integer count;
        // 总时长
        public Integer duration;
        // 课程名称
        public String name;
        // 贡献者
        public String author;
        // 学习人数
        public Integer vv;
        // 课程Id
        public String lessonId;

    }
}
